<template>
  <div class="container">
    <el-card class="my-box-card" v-if="isDisplayAuditBtn && currentComParams.openType === 'audit'">
      <work-flow-audit-button-com
        :workitemInsId="currentComParams.workitemInsId"
        :busiRelateData="busiRelateData"
        :curUserId="curUserId"
        :curUserName="curUserName"
        :busiOrgId="record.busiOrgId"
        :busiCorpId="record.busiCorpId"
        @auditBefore="auditBefore"
        @auditAfter="auditAfter"></work-flow-audit-button-com>
      <div style="display: inline-block;">
<!--        扩展按钮栏-->
<!--        <div class="auditBtnDiv">-->
<!--          <el-button size="small" type="primary">测试</el-button>-->
<!--        </div>-->
      </div>
    </el-card>

<!--    <el-card class="my-box-card" v-if="isDisplayAuditBtn && openType === 'audit' && handlingType === '02'">-->
<!--      <work-flow-add-signer-audit-button-com-->
<!--        :workitemInsId="workitemInsId"-->
<!--        :busiRelateData="busiRelateData"-->
<!--        @auditBefore="auditBefore"-->
<!--        @auditAfter="auditAfter"></work-flow-add-signer-audit-button-com>-->
<!--    </el-card>-->
    <!--<el-card class="my-box-card" v-if="isDisplayAuditViewBtn">-->
      <!--<div class="auditBtnDiv">-->
        <!--<el-button size="small" type="primary" @click="auditWithdraw()">撤回</el-button>-->
      <!--</div>-->
    <!--</el-card>-->
    <el-card class="box-card">
      <work-flow-circulation-com :procInstanceId="record.processInsId"></work-flow-circulation-com>
    </el-card>
    <el-card class="my-box-card">
      <el-form ref="dataForm" :model="record" label-position="top" size="small"  label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="record.title" placeholder="标题" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="my-box-card">
      <work-flow-audit-history-com :busiTypeId="busiTypeId" :busiId="record.id"></work-flow-audit-history-com>
    </el-card>
  </div>
</template>
<script>
  import { closeTab } from '@/utils/index'
  import { workFlowCirculationCom, workFlowAuditHistoryCom,
    workFlowAuditButtonCom, workFlowAddSignerAuditButtonCom } from '@/views/workFlowManage/busiWorkFlowComponents'

  export default {
    name: 'wfBusiDemoView',
    data() {
      return {
        isDisplayAuditBtn: false,
        isDisplayAuditViewBtn: false,
        busiTypeId: 'wfBusiDemo',
        curUserId: this.$store.state.user.userid,
        curUserName: this.$store.state.user.name,
        curOrgId: this.$store.state.user.orgId,
        processInsId: '',
        record: {
          id: '',
          title: '',
          busiUserId: '',
          busiOrgId: '',
          busiCorpId: '',
          processInsId: ''
        },
        busiRelateData: [],
        currentComParams: {}
      }
    },
    props: ['componentCustomProps'],
    components: {
      workFlowCirculationCom,
      workFlowAuditHistoryCom,
      workFlowAuditButtonCom,
      workFlowAddSignerAuditButtonCom
    },
    created() {
      if (this.componentCustomProps) {
        this.currentComParams = this.componentCustomProps
      } else {
        this.currentComParams = this.$route.query
      }
      this.fetchData()
      // this.auditWithdrawEnable()
    },
    watch: {
      componentCustomProps() { // 普通的watch监听通的watch监听
        this.fetchData()
        // this.auditWithdrawEnable()
      }
    },
    methods: {
      fetchData() {
        let queryArgs = {
          condition: [
            {
              property: 'id',
              value: '',
              type: 'EQ'
            }
          ]
        }
        if (this.currentComParams && this.currentComParams.busiId) {
          queryArgs.condition[0].value = this.currentComParams.busiId
        }

        this.$store.dispatch('WfBusiDemoSelect', queryArgs).then(res => {
          this.loading = false
          if (res && res.success && res.data.records[0]) {
            this.record = res.data.records[0]
            this.record.busiCorpId = ''
            this.busiRelateData = []
            let relateData = {
              'relateKey': 'type',
              'relateValue': '1'
            }
            this.busiRelateData.push(relateData)

            // 加载审核按钮
            this.isDisplayAuditBtn = true
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      auditBefore(btnInfo, busiRelateData) {
        // alert(btnId)
        // 清空数组，不要使用busiRelateData = [] 生成新的对象
        busiRelateData.length = 0
        busiRelateData.push({
          'relateKey': 'type',
          'relateValue': '0'
        })
      },
      auditAfter(btnInfo, args) {
        //
        let btnId = btnInfo.buttonId
        if (btnId.endsWith('_doAddSigner') || btnId.endsWith('_doDocumentCirculation')) { // 前加签、公文流转
          //
          this.closeComponent()
        } else if (btnId.endsWith('_doPass') || btnId.endsWith('_doReview') || btnId.endsWith('_doRushToSign')) { // 同意、审阅、抢签
          args.id = this.record.id

          let methodName = btnId.endsWith('_doRushToSign') ? 'WfBusiDemoRushToSign' : 'WfBusiDemoPass'
          this.$store.dispatch(methodName, args).then(res => {
            if (res && res.success) {
              this.closeComponent()
            } else {
              this.$message({
                type: 'danger',
                message: '操作异常!' + res.message
              })
            }
          }).catch((err) => {
            console.log(err)
          })
        } else if (btnId.endsWith('_doReject')) { // 驳回到申请人
          args.id = this.record.id
          this.$store.dispatch('WfBusiDemoReject', args).then(res => {
            if (res && res.success) {
              this.closeComponent()
            } else {
              this.$message({
                type: 'danger',
                message: '驳回异常!' + res.message
              })
            }
          }).catch((err) => {
            console.log(err)
          })
        } else if (btnId.endsWith('_doRead')) { // 单据已阅
          args.id = this.record.id
          this.$store.dispatch('WfBusiDemoRead', args).then(res => {
            if (res && res.success) {
              this.closeComponent()
            } else {
              this.$message({
                type: 'danger',
                message: '已阅异常!' + res.message
              })
            }
          }).catch((err) => {
            console.log(err)
          })
        } else {
          this.closeComponent()
        }
      },
      closeComponent() {
        // 调用父组件关闭
        try {
          this.$emit('closeComponent')
        } catch (e) {
          //
        }
        // tab打开方式关闭
        try {
          closeTab(this.$route)
        } catch (e) {
          //
        }
      }
      // auditWithdrawEnable() { // 审核人撤回到当前节点
      //   //
      //   if (this.openType === 'auditView' && this.handlingType === '01') { // 主审已办查看
      //     let args = {}
      //     args.userId = this.curUserId
      //     args.workitemInsId = this.workitemInsId
      //     this.$store.dispatch('WfBusiProcAuditWithdrawEnable', args).then(res => {
      //       if (res && res.success) {
      //         this.isDisplayAuditViewBtn = true
      //       }
      //     }).catch((err) => {
      //       console.log(err)
      //     })
      //   }
      // },
      // auditWithdraw() { // 审核人撤回到当前节点
      //   this.$confirm('确定撤回吗?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     let args = {}
      //     args.userId = this.curUserId
      //     args.workitemInsId = this.workitemInsId
      //     this.$store.dispatch('WfBusiProcAuditWithdraw', args).then(res => {
      //       if (res && res.success) {
      //         this.closeComponent()
      //       } else {
      //         this.$message({
      //           type: 'danger',
      //           message: '撤回异常!' + res.message
      //         })
      //       }
      //     }).catch((err) => {
      //       console.log(err)
      //     })
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消撤回'
      //     })
      //   })
      // }
    }
  }
</script>
<style scoped>
  .container{
    padding: 0 10px;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .my-box-card >>> .el-card__body {
    padding: 5px;
  }
  .auditBtnDiv {
    height: 45px;
    overflow: auto;
  }
  .auditBtnDiv >>> .el-button {
    margin: 5px 0px 5px 7px;
  }
</style>
